package com.merlin.demo_wellshape.controller;

import com.merlin.demo_wellshape.dto.DailyPlanDTO;
import com.merlin.demo_wellshape.dto.ExercisePlanDTO;
import com.merlin.demo_wellshape.pojo.DailyPlan;
import com.merlin.demo_wellshape.pojo.ResponseMessage;
import com.merlin.demo_wellshape.service.DailyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api")
public class DailyPlanController {

    @Autowired
    private DailyPlanService dailyPlanService;

    // 获取用户所有计划
    @GetMapping("/plans") // URL：localhost:8080/api/plans  method:get
    public ResponseMessage<List<DailyPlanDTO>> getPlansByUserId(@RequestParam int userId) {
        List<DailyPlanDTO> dailyPlanDTOs = dailyPlanService.getAllPlansByUserId(userId);
        return new ResponseMessage<>(200, "获取所有计划成功~", dailyPlanDTOs);
    }

    // 添加计划
    @PostMapping("/plans") // URL：localhost:8080/api/plans  method:post
    public ResponseMessage<DailyPlanDTO> addPlan(
            @RequestParam int userId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate planDate,
            @RequestParam String planName,
            @RequestParam String quantity,
            @RequestParam(required = false, defaultValue = "false") boolean completed) {
        try {
            DailyPlan dailyPlan = dailyPlanService.addPlan(userId, planDate, planName, quantity, completed);
            DailyPlanDTO dailyPlanDTO = dailyPlanService.convertToDTO(dailyPlan);
            return new ResponseMessage<>(201, "计划添加成功~", dailyPlanDTO);
        } catch (IllegalArgumentException e) {
            return new ResponseMessage<>(400, "计划添加失败~" + e.getMessage(), null);
        }
    }

    // 修改计划
    @PutMapping("/plans/{planId}") // URL：localhost:8080/api/plans/{planId}  method:put
    public ResponseMessage<ExercisePlanDTO> updatePlan(
            @PathVariable int planId,
            @RequestParam String planName,
            @RequestParam String quantity,
            @RequestParam(required = false, defaultValue = "false") boolean completed) {
        try {
            ExercisePlanDTO updatedPlanDTO = dailyPlanService.updatePlan(planId, planName, quantity, completed);
            return new ResponseMessage<>(200, "计划修改成功~", updatedPlanDTO);
        } catch (IllegalArgumentException e) {
            return new ResponseMessage<>(400, "计划修改失败~" + e.getMessage(), null);
        } catch (Exception e) {
            return new ResponseMessage<>(500, "服务器内部错误~" + e.getMessage(), null);
        }
    }

    // 删除计划
    @DeleteMapping("/plans") // URL：localhost:8080/api/plans  method:delete
    public ResponseMessage<Void> deletePlan(@RequestParam int planId) {
        try {
            dailyPlanService.deletePlan(planId);
            return new ResponseMessage<>(200, "计划删除成功~", null);
        } catch (IllegalArgumentException e) {
            return new ResponseMessage<>(400, "计划删除失败~" + e.getMessage(), null);
        } catch (Exception e) {
            return new ResponseMessage<>(500, "服务器内部错误~" + e.getMessage(), null);
        }
    }
}
